import $ from "jquery";
import {url} from "../../util/config";
import {message} from "antd";
import login from "../login";

function getNewLogin() {
    return getDeptBind()
}

export default function getDeptBind() {
    let deptsBind = [];
    let token = localStorage.getItem("token")

    $.ajax({
        async:false,
        headers: {
            'Accept': "application/json",
                'Authorization': token
        },
        url: url.api_url+`DeptBindRole/GetDept`,
        type: 'GET',
        success: function (result) {
            let tmp = result.result;
            if(tmp=="Token過期請重新登錄") {
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                deptsBind = getNewLogin()
            }else {
                deptsBind = tmp.map(item=>{
                    return {
                        key:item.DEPT_ID,//部门ID
                        name:item.DEPT_NAME,  //部门名称
                        no:item.DEPT_NO,  //部门编号
                        createUser:item.CREATE_USER,
                        createTime:item.CREATE_DATE,
                        updateUser:item.UPDATE_USER,
                        updateDate:item.UPDATE_DATE
                    }
                });
                sessionStorage.setItem("deptsBind",JSON.stringify(deptsBind))
            }
        },
        error:function (error) {
            message.error("请求异常")
        }
    })
    return deptsBind;

}

